package com.joven.shelltest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joven.shelltest.entity.SysUserModel;
import com.joven.shelltest.entity.WordPartMappingModel;
import com.joven.shelltest.mapper.SysUserMapper;
import com.joven.shelltest.service.SysUserService;
import com.joven.shelltest.service.WordPartMappingService;
import com.joven.shelltest.utils.encrypt.AES;
import com.joven.shelltest.utils.mybatisplusdim.AnalyzerUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wzj
 * @Date 2023-07-27 08:34
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserModel> implements SysUserService {

    @Cacheable(value = "user", cacheManager = "cacheManager")
    @Override
    public List<SysUserModel> getUsrByStatus(Integer status) {
        System.out.println("查询数据库");
        return this.list(new LambdaQueryWrapper<SysUserModel>().eq(SysUserModel::getStatus, status));
    }

    private final WordPartMappingService wordPartMappingService;

    @Override
    public void insertIntoValuesAndMapping(SysUserModel sysUserModel) {
        this.save(sysUserModel);
        List<String> testMobile = AnalyzerUtil.partSegmentationList(sysUserModel.getEmail(), 18);
        if (CollectionUtils.isNotEmpty(testMobile)) {
            testMobile.forEach(s -> {
                WordPartMappingModel wordPartMapping = new WordPartMappingModel();
                wordPartMapping.setTableName("sys_user");
                wordPartMapping.setRecordId(sysUserModel.getId());
                wordPartMapping.setFieldName("email");
                try {
                    wordPartMapping.setAnalyzerEncrypt(AES.encrypt(s));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                wordPartMappingService.save(wordPartMapping);
            });
        }
    }

    @Override
    public List<SysUserModel> getUsrDim(String email) {
        List<SysUserModel> dataInfos;
        try {
            List<WordPartMappingModel> records = wordPartMappingService.list(new LambdaQueryWrapper<WordPartMappingModel>()
                    .eq(WordPartMappingModel::getTableName, "sys_user")
                    .eq(WordPartMappingModel::getFieldName, "email")
                    .like(WordPartMappingModel::getAnalyzerEncrypt, AES.encrypt(email)));
            if (CollectionUtils.isEmpty(records)) {
                return null;
            }
            List<Integer> ids = records.stream().map(WordPartMappingModel::getRecordId).collect(Collectors.toList());
            dataInfos = this.list(new LambdaQueryWrapper<SysUserModel>().in(SysUserModel::getId, ids));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return dataInfos;
    }


}
